﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChatData;
using Server;
using System.IO;

namespace Client
{
    
    //Messages and Files Reply with an ordinary message. Can do DM (possible to implement??)
    //Need to send info to server with Client and call Reset() when done.
    public partial class Edit : UserControl
    {
        public ActiveUserStore<Record> Activeusers { get; set; }

        private string destination;
        private string file;
        public string Status { get; set; }
        public string User { get; set; }
        public string FilePath {get; private set;}
        public ChatServerClient Client { get; set; }
        public RequestType Event { get; private set; }
        public Edit()
        {
            InitializeComponent();
            Event = RequestType.Message; //set to default
        }

        // called after each Send method
        private void Reset()
        {
            this.kryptonHeaderGroup1.ValuesPrimary.Description = "New Message";
            this.kryptonHeaderGroup1.ValuesSecondary.Heading = "";
            this.kryptonRichTextBox1.Text = "";
            destination = "";
            file = "";
        }
        #region Setup Normal
        public void NewFile()
        {
            kryptonRichTextBox1.Enabled = false;
            kryptonRichTextBox1.Clear();
            this.kryptonHeaderGroup1.ValuesPrimary.Description = "Send File";
            this.kryptonHeaderGroup1.ValuesSecondary.Heading = "";
            this.Event = RequestType.File;
        }
        public void NewPost()
        {
            kryptonRichTextBox1.Enabled = true;
            kryptonRichTextBox1.Clear();
            this.kryptonHeaderGroup1.ValuesPrimary.Description = "New Message";
            this.kryptonHeaderGroup1.ValuesSecondary.Heading = "";
            this.Event = RequestType.Message;
            this.kryptonRichTextBox1.Focus();
        }
        public void NewDM()
        {
            kryptonRichTextBox1.Enabled = true;
            kryptonRichTextBox1.Clear();
            this.kryptonHeaderGroup1.ValuesPrimary.Description = "Direct Message";
            this.kryptonHeaderGroup1.ValuesSecondary.Heading = "";
            this.Event = RequestType.DM;
            this.kryptonRichTextBox1.Focus();
            this.destination = getDestination();
            this.kryptonHeaderGroup1.ValuesSecondary.Heading = "To: " + destination;
        }
        #endregion
        #region Setup Reply -DM
        public void Reply(String thedestination)
        {
            destination = thedestination;
            kryptonRichTextBox1.Enabled = true;
            kryptonRichTextBox1.Clear();
            this.kryptonHeaderGroup1.ValuesPrimary.Description = "Direct Message";
            this.kryptonHeaderGroup1.ValuesSecondary.Heading = "To: " + destination;
            this.Event = RequestType.DM;
            this.kryptonRichTextBox1.Focus();
        }
        public void Reply(DMPost thedata)
        {
            destination = thedata.Origin;
            kryptonRichTextBox1.Enabled = true;
            kryptonRichTextBox1.Clear();
            this.kryptonHeaderGroup1.ValuesPrimary.Description = "Direct Message";
            this.kryptonHeaderGroup1.ValuesSecondary.Heading= "To: " + destination;
            this.Event = RequestType.DM;
            this.kryptonRichTextBox1.Focus();
        }
        #endregion
        #region Send
        private void Send()
        {
            switch (this.Event)
            {
                case RequestType.File: SendFile();
                    break;
                case RequestType.Message: SendMessage();
                    break;
                case RequestType.DM: sendDM();
                    break;
            }
        }
        private void SendMessage()
        {
            Client.Post(Utils.GetLocalIPAddress(), User, Status, kryptonRichTextBox1.Text);
            Reset();
        }
        private void SendFile()
        {
            FileInfo thefile = new FileInfo(FilePath);
            if (thefile.Exists == true)
            {
                byte[] file = System.IO.File.ReadAllBytes(FilePath);
                Array.Reverse(file);//scramble
                Client.PostFile(Utils.GetLocalIPAddress(), User, Status, file, thefile.Name, destination);
                Reset();
            }
        }
        private void sendDM()
        {
            if (destination != "")
            {
                Client.DM(Utils.GetLocalIPAddress(), User, Status, kryptonRichTextBox1.Text, destination);
            }
            else
            {
                this.destination = getDestination();
                Client.DM(Utils.GetLocalIPAddress(), User, Status, kryptonRichTextBox1.Text, destination);
            }
            Reset();
        }
        #endregion
        #region Gets
        public string getDestination()
        {
            Activeusers = new ActiveUserStore<Record>(Client.getActiveUsers());
            using (Users dialog = new Users(Activeusers))
            {
                DialogResult result = dialog.ShowDialog();
                if (result == DialogResult.Yes)
                {
                    destination = dialog.Selection;
                    return destination;
                }
            }
            return "";
        }
        public string getFile()
        {
            this.openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            System.Windows.Forms.DialogResult result = this.openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                file= this.openFileDialog1.FileName;
                return file;
            }
            return "";
        }
        #endregion
        #region ButtonSpecs
        private void buttonSpecHeaderGroup1_Click(object sender, EventArgs e)
        {

            this.NewFile();
            this.FilePath= getFile();
            if (this.FilePath != "")// blank signifies a cancel
            {
                string destination = getDestination();
                this.kryptonHeaderGroup1.ValuesSecondary.Description = " Send " + FilePath + " To: " + destination;
                Send();
            }
            else
            {
                Reset();
            }
            return;
        }
        //DM
        private void buttonSpecHeaderGroup2_Click(object sender, EventArgs e)
        {
            this.NewDM();
            this.kryptonHeaderGroup1.ValuesPrimary.Description = "DM";
            this.kryptonHeaderGroup1.ValuesSecondary.Heading = "To: " + destination;
        }
        private void buttonSpecHeaderGroup3_Click(object sender, EventArgs e)
        {
            Reset();
        }
        private void buttonSpecHeaderGroup4_Click(object sender, EventArgs e)
        {
            this.NewPost();
        }
        #endregion

        private void kryptonRichTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter: Send();
                    break;

            }
            e.SuppressKeyPress = false;
        }

        

        
    }
}
